﻿using System.Security.Cryptography;
using System.Text;

namespace 发卡工具
{
    public partial class Activate : Form
    {
        public Activate()
        {
            InitializeComponent();
        }

        private static string ComputeSha256Hash(string input)
        {
            byte[] inputBytes = Encoding.UTF8.GetBytes(input);
            byte[] hashBytes = SHA256.HashData(inputBytes);

            var builder = new StringBuilder();
            foreach (byte t in hashBytes)
            {
                builder.Append(t.ToString("x2"));
            }

            return builder.ToString();
        }

        private void Activated_Click(object sender, EventArgs e)
        {
            if (RequestCode.Text == "")
            {
                MessageBox.Show("请输入请求码!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            byte[] requestCodeBytes = Convert.FromBase64String(RequestCode.Text);
            string requestCode = Encoding.UTF8.GetString(requestCodeBytes);
            string[] parts = requestCode.Split(',');
            string[] cpuId = parts[0].Split(':');
            string codeResult = $"cpuHash:{ComputeSha256Hash(cpuId[1])},requestTime:{parts[1]},hash:HASH256";
            ActivateCode.Text = Convert.ToBase64String(Encoding.UTF8.GetBytes(codeResult));
            Clipboard.SetText(ActivateCode.Text);
            MessageBox.Show("激活码已生成，请将激活码发送给买家！激活码已经复制到剪切板！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            RequestCode.Text = "";
        }
    }
}
